package com.hsyt.pms.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hsyt.pfc.support.rpc.PageResult;
import com.hsyt.pfc.support.util.EnumToDataUtil;
import com.hsyt.pfc.support.util.LoggerUtil;
import com.hsyt.pms.api.common.PltAuth;
import com.hsyt.pms.api.common.PltLog;
import com.hsyt.pms.api.common.ResultMessage;
import com.hsyt.pms.api.dto.system.SysFunctionDto;
import com.hsyt.pms.api.enums.AbleStatus;
import com.hsyt.pms.api.enums.FunctionType;
import com.hsyt.pms.api.enums.Level;
import com.hsyt.pms.api.vo.system.SysFunctionVo;
import com.hsyt.pms.controller.base.BaseController;
import com.hsyt.pms.sys.dubbo.SysFunctionService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName: SysFunctionController
 * @Description: 菜单服务控制层
 * @author: wangze@hsyuntai.com
 * @date: 2019/1/29 18:19
 */
@RestController
@RequestMapping("/pms/system/function")
public class SysFunctionController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysFunctionController.class);

    @Reference(group = "pmsSystem", registry = "pms", version = "0.0.1")
    private SysFunctionService sysFunctionService;

    @RequestMapping(value = "/page")
    @PltAuth(auth = "ROLE_MENU_MG")
    public ModelAndView page(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(AbleStatus.class);
        model.put("statusComboData", statusComboData);
        String levelComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Level.class);
        model.put("levelComboData", levelComboData);
        return new ModelAndView("system/pageFunction");
    }

    @RequestMapping(value = "/toEditForm")
    @PltAuth(auth = "ROLE_MENU_MG_ADD,ROLE_MENU_MG_MODIFY")
    public ModelAndView toEditForm(ModelMap model, @RequestParam("formType") String formType) {
        model.put("formType", formType);
        return new ModelAndView("system/formFunction");
    }

    @PltLog(log = "查询菜单")
    @PltAuth(auth = "ROLE_MENU_MG")
    @RequestMapping(value = "/findPageData")
    public @ResponseBody
    PageResult findPageData(@RequestBody SysFunctionDto dto) {
        PageResult<SysFunctionVo> pageResult = new PageResult<SysFunctionVo>();
        try {
            pageResult = sysFunctionService.searchPageDataByDto(dto);
        } catch (Exception e) {
            pageResult.setResult(false);
            String message = MessageFormat.format("SysFunctionController.searchPageData error: {0}", e.getMessage());
            pageResult.setMessage(message);
            LoggerUtil.error(LOGGER, message);
        }
        return pageResult;
    }

    @PltLog(log = "新增菜单")
    @PltAuth(auth = "ROLE_MENU_MG_ADD")
    @RequestMapping(value = "/add")
    public @ResponseBody
    ResultMessage add(@RequestBody SysFunctionDto function, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "新增成功");
        try {
            function.setFunctionType(FunctionType.MENU.getCode());
            if (StringUtils.isBlank(function.getStatus())) {
                function.setStatus(AbleStatus.ENABLED.getCode());
            }
            if (sysFunctionService.getFunctionCount(function) > 0) {
                result.change(false, "存在记录！");
                return result;
            }
            sysFunctionService.addFunction(function);
        } catch (Exception e) {
            result.change(false, "保存失败 , 错误信息:" + e.getMessage());
        }
        return result;
    }

    @PltLog(log = "编辑菜单")
    @PltAuth(auth = "ROLE_MENU_MG_MODIFY")
    @RequestMapping(value = "/update")
    public @ResponseBody
    ResultMessage update(@RequestBody SysFunctionDto function, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "编辑成功");
        try {
            sysFunctionService.updateFunctionByKeySelective(function);
        } catch (Exception e) {
            result.change(false, "保存失败 , 错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysFunctionController-update error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "根据ds集合删除数据项")
    @PltAuth(auth = "ROLE_MENU_MG_DELETE")
    @RequestMapping(value = "/deleteByIds")
    public @ResponseBody
    ResultMessage deleteByIds(@RequestBody List<Long> ids) {
        ResultMessage result = new ResultMessage(true, "成功");
        if (ids == null || ids.size() <= 0) {
            result.change(false, "入参有误" + ids);
            return result;
        }
        try {
            for (Iterator<Long> iterator = ids.iterator(); iterator.hasNext();) {
                Long id = iterator.next();
                SysFunctionDto dto = new SysFunctionDto();
                dto.setId(id);
                dto.setStatus(AbleStatus.DISABLED.getCode());
                sysFunctionService.updateFunctionByKeySelective(dto);
            }
        } catch (Exception e) {
            result.change(false, "操作失败，错误信息:" + e.getMessage());
            LoggerUtil.error(e, LOGGER, "SysFunctionController.delete error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "更改菜单状态")
    @RequestMapping(value = "/changeState")
    public @ResponseBody
    ResultMessage changeState(SysFunctionDto function, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        if (function.getId() == null) {
            result.change(false, "参数中id为空");
            return result;
        }
        if (StringUtils.isBlank(function.getStatus())) {
            function.setStatus(AbleStatus.DISABLED.getCode());
        }
        try {
            sysFunctionService.updateFunctionByKeySelective(function);
        } catch (Exception e) {
            result.change(false, "操作失败，错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysFunctionController-changeState error: {0}", e.getMessage());
        }
        return result;
    }

}
